package moteur;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeMap;

public abstract class NormalSearch implements Search{
	/**
	 *  this class is a base class for tfitf search
	 *  it implements Search interface
	 *  and defines some common actions for NearSearch and NearSearchDoubleIndex
	 */
	private TreeMap<Integer, HashSet<String> > priorityList=null;
	private int lastRead=0;
	//show first nbResults in Search
	private int nbResults=20;
	private ArrayList<Integer> keyArray=new ArrayList<Integer>();
	
	public abstract void rankDocs(LinkedHashSet<String> lhs,HashSet<String> filenames);
	public abstract TreeMap<Integer, HashSet<String> > getPriorityList();
	
	@Override
	public Set<String> get(LinkedHashSet<String> lhs) {
		/**
		 *  interface defined in Search
		 */
		// TODO Auto-generated method stub
		if(this.priorityList==null)
		{
			this.priorityList=this.getPriorityList();
			keyArray.addAll(this.priorityList.keySet());
		}
		
		//System.out.println(this.priorityList.get(1).size());
		for(; this.lastRead<keyArray.size(); this.lastRead++)
		{
			if(lhs.size()>=this.getNbResults())
			{
				break;
			}
			else
			{
				System.out.println(keyArray.get(this.lastRead)+"  terms:");
				this.rankDocs(lhs, this.priorityList.get(keyArray.get(this.lastRead)));
			}
		}
		return lhs;
	}
	
	@Override
	public Set<String> get() {
		// TODO Auto-generated method stub
		LinkedHashSet<String> lhs=new LinkedHashSet<String>();
		this.get(lhs);
		return lhs;
	}
	
	public int getNbResults() {
		return nbResults;
	}

	public void setNbResults(int nbResults) {
		this.nbResults = nbResults;
	}

}
